<!--
 * @Description: 购物车
 -->
<template>
  <div class="shoppingCart">
    <!-- 购物车头部 -->
    <div class="cart-header">
      <div class="cart-header-content">
        <p>
          购物车
        </p>
        <span>以最终下单为准，请尽快结算</span>
      </div>
    </div>
    <!-- 购物车头部END -->

    <!-- 购物车主要内容区 -->
    <div class="content" v-if="this.$store.getters['shoppingCart/getShoppingCart'].length > 0">
      <ul>
        <!-- 购物车表头 -->
        <li class="header">
          <div class="pro-check">
            <el-checkbox v-model="isAllCheck">全选</el-checkbox>
          </div>
          <div class="pro-img"></div>
          <div class="pro-name">景点名称</div>
          <div class="pro-price">单价</div>
          <div class="pro-num">数量</div>
          <div class="pro-total">小计</div>
          <div class="pro-action">操作</div>
        </li>
        <!-- 购物车表头END -->

        <!-- 购物车列表 -->
        <li
          class="product-list"
          v-for="(item, index) in this.$store.getters['shoppingCart/getShoppingCart']"
          :key="item.pid"
        >
          <div class="pro-check">
            <el-checkbox
              :value=item.productCheck
              @change="checkChange($event, index)"
            ></el-checkbox>
          </div>
          <div class="pro-img">
            <router-link
              :to="{
                path: '/product/details',
                query: { productID: item.productPid },
              }"
            >
              <!--                            <img :src=item.imgUrl[0].imgUrl />-->
            </router-link>
          </div>
          <div class="pro-name">
            <router-link
              :to="{
                path: '/product/details',
                query: { productID: item.productPid },
              }"
            >{{ item.productName }}
            </router-link
            >
          </div>
          <div class="pro-price">{{ item.price }}元</div>
          <div class="pro-num">
            <el-input-number
              size="small"
              :value="item.quanity"
              @change="handleChange($event, index, item.productPid)"
              :min="1"
              :max="item.maxNum"
            ></el-input-number>
          </div>
          <div class="pro-total pro-total-in">
            {{ item.price * item.quanity }}元
          </div>
          <div class="pro-action">
            <el-popover placement="right">
              <p>确定删除吗？</p>
              <div style="text-align: right; margin: 10px 0 0">
                <el-button
                  type="primary"
                  size="mini"
                  @click="deleteItem($event, item.id, item.pid)"
                >确定
                </el-button
                >
              </div>
              <i
                class="el-icon-error"
                slot="reference"
                style="font-size: 18px"
              ></i>
            </el-popover>
          </div>
        </li>
        <!-- 购物车列表END -->
      </ul>
      <div style="height: 20px; background-color: #f5f5f5"></div>
      <!-- 购物车底部导航条 -->
      <div class="cart-bar">
        <div class="cart-bar-left">
          <span>
            <router-link to="/product">继续浏览</router-link>
          </span>
          <span class="sep">|</span>
          <span class="cart-total">
            共
            <span class="cart-total-num">1</span> 处景点，已选择
            <span class="cart-total-num">{{ this.$store.getters["shoppingCart/getCheckNum"] }}</span> 件
          </span>
        </div>
        <div class="cart-bar-right">
          <span>
            <span class="total-price-title">合计：</span>
            <span class="total-price">{{ this.$store.getters["shoppingCart/getTotalPrice"] }}元</span>
          </span>
          <router-link :to="1 > 0 ? '/shopping/details' : ''">
            <div
              :class="1 > 0 ? 'btn-primary' : 'btn-primary-disabled'"
            >
              生成订单
            </div>
          </router-link>
        </div>
      </div>
      <!-- 购物车底部导航条END -->
    </div>
    <!-- 购物车主要内容区END -->

    <!-- 购物车为空的时候显示的内容 -->
    <div v-else class="cart-empty">
      <div class="empty">
        <h2>您的购物车还是空的！</h2>
      </div>
    </div>
    <!-- 购物车为空的时候显示的内容END -->
  </div>
</template>
<script>
import {mapActions} from "vuex";
import {mapGetters} from "vuex";

export default {
  data() {
    return {};
  },
  mounted() {
    console.log("我的购物车",)
  },
  computed: {
    ...mapGetters([
      "shoppingCart/getShoppingCart",
      "shoppingCart/getCheckNum",
      "shoppingCart/getTotalPrice",
      "shoppingCart/getNum",
    ]),
    isAllCheck: {
      get() {
        return this.$store.getters["shoppingCart/getIsAllCheck"];
      },
      set(val) {
        this.$store.commit("shoppingCart/checkAll", val)
      },
    },
  },
  methods: {
    ...mapActions(["shoppingCart/updateShoppingCart", "shoppingCart/deleteShoppingCart", "shoppingCart/checkAll"]),
    // 修改景点数量的时候调用该函数
    handleChange(currentValue, key, productID) {
      // 当修改数量时，默认勾选
      this.$store.commit("shoppingCart/updateShoppingCart", {key: key, prop: "productCheck", val: true})
      // this.updateShoppingCart({key: key, prop: "productCheck", val: true});
      // 向后端发起更新购物车的数据库信息请求
      this.$axios
        .post("/cat/userCardProduct/updateShoppingCart", {
          userPid: this.$store.getters["catUser/getUser"].pid,
          cardPid: this.$store.getters["shoppingCart/getShoppingCart"][0].cardPid,
          productPid: productID,
          quanity: currentValue,
        })
        .then((res) => {
          console.log(key + " key " + currentValue + " currentValue ");
          switch (res.data.code) {
            case 200:
              // 200代表更新成功
              // 更新vuex状态
              this.$store.commit("shoppingCart/updateShoppingCart", {
                key: key,
                prop: "quanity",
                val: currentValue,
              })

              // this.updateShoppingCart({
              //   key: key,
              //   prop: "quanity",
              //   val: currentValue,
              // });
              // 提示更新成功信息
              // this.notifySucceed(res.data.msg);
              break;
            default:
              // 提示更新失败信息
              // this.notifyError(res.data.msg);
          }
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    checkChange(val, key) {
      // 更新vuex中购物车景点是否勾选的状态
      this.$store.commit("shoppingCart/updateShoppingCart", {key: key, prop: "productCheck", val: val})
      // this.updateShoppingCart({key: key, prop: "productCheck", val: val});
    },
    // 向后端发起删除购物车的数据库信息请求
    deleteItem(e, id, productID) {
      let userInfo = JSON.parse(this.$store.getters["catUser/getUser"])
      this.$axios
        .post("/cat/userCardProduct/deleteShoppingCart", {
          userPid: userInfo.pid,
          productPid: productID,
        })
        .then((res) => {
          switch (res.data.code) {
            case 200:
              // 更新vuex状态
              // this.deleteShoppingCart(id);
              this.$store.commit('shoppingCart/deleteShoppingCart', productID)
              // this.deleteShoppingCart(productID);
              // 提示删除成功信息
              // this.notifySucceed(res.data.msg);
              this.$message({
                showClose: false,
                message: "删除成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                },
              });
              break;
            default:
              // 提示删除失败信息
              this.$message({
                showClose: false,
                message: "删除失败",
                type: "fail",
                duration: 1000,
                onClose: () => {
                },
              });
          }
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
  },
};
</script>
<style scoped>
.shoppingCart {
  background-color: #f5f5f5;
  padding-bottom: 20px;
}

/* 购物车头部CSS */
.shoppingCart .cart-header {
  height: 64px;
  border-bottom: 2px solid #000000;
  background-color: #fff;
  margin-bottom: 20px;
}

.shoppingCart .cart-header .cart-header-content {
  width: 1225px;
  margin: 0 auto;
}

.shoppingCart .cart-header p {
  font-size: 28px;
  line-height: 58px;
  float: left;
  font-weight: normal;
  color: #424242;
}

.shoppingCart .cart-header span {
  color: #757575;
  font-size: 12px;
  float: left;
  height: 20px;
  line-height: 20px;
  margin-top: 30px;
  margin-left: 15px;
}

/* 购物车头部CSS END */

/* 购物车主要内容区CSS */
.shoppingCart .content {
  width: 1225px;
  margin: 0 auto;
  background-color: #fff;
}

.shoppingCart .content ul {
  background-color: #fff;
  color: #424242;
  line-height: 85px;
}

/* 购物车表头及CSS */
.shoppingCart .content ul .header {
  height: 85px;
  padding-right: 26px;
  color: #424242;
}

.shoppingCart .content ul .product-list {
  height: 85px;
  padding: 15px 26px 15px 0;
  border-top: 1px solid #e0e0e0;
}

.shoppingCart .content ul .pro-check {
  float: left;
  height: 85px;
  width: 110px;
}

.shoppingCart .content ul .pro-check .el-checkbox {
  font-size: 16px;
  margin-left: 24px;
}

.shoppingCart .content ul .pro-img {
  float: left;
  height: 85px;
  width: 120px;
}

.shoppingCart .content ul .pro-img img {
  height: 80px;
  width: 80px;
}

.shoppingCart .content ul .pro-name {
  float: left;
  width: 380px;
}

.shoppingCart .content ul .pro-name a {
  color: #424242;
}

.shoppingCart .content ul .pro-name a:hover {
  color: #000000;
}

.shoppingCart .content ul .pro-price {
  float: left;
  width: 140px;
  padding-right: 18px;
  text-align: center;
}

.shoppingCart .content ul .pro-num {
  float: left;
  width: 150px;
  text-align: center;
}

.shoppingCart .content ul .pro-total {
  float: left;
  width: 120px;
  padding-right: 81px;
  text-align: right;
}

.shoppingCart .content ul .pro-total-in {
  color: #000000;
}

.shoppingCart .content ul .pro-action {
  float: left;
  width: 80px;
  text-align: center;
}

.shoppingCart .content ul .pro-action i:hover {
  color: #000000;
}

/* 购物车表头及CSS END */

/* 购物车底部导航条CSS */
.shoppingCart .cart-bar {
  width: 1225px;
  height: 50px;
  line-height: 50px;
  background-color: #fff;
}

.shoppingCart .cart-bar .cart-bar-left {
  float: left;
}

.shoppingCart .cart-bar .cart-bar-left a {
  line-height: 50px;
  margin-left: 32px;
  color: #757575;
}

.shoppingCart .cart-bar .cart-bar-left a:hover {
  color: #000000;
}

.shoppingCart .cart-bar .cart-bar-left .sep {
  color: #eee;
  margin: 0 20px;
}

.shoppingCart .cart-bar .cart-bar-left .cart-total {
  color: #757575;
}

.shoppingCart .cart-bar .cart-bar-left .cart-total-num {
  color: #000000;
}

.shoppingCart .cart-bar .cart-bar-right {
  float: right;
}

.shoppingCart .cart-bar .cart-bar-right .total-price-title {
  color: #000000;
  font-size: 14px;
}

.shoppingCart .cart-bar .cart-bar-right .total-price {
  color: #000000;
  font-size: 30px;
}

.shoppingCart .cart-bar .cart-bar-right .btn-primary {
  float: right;
  width: 200px;
  text-align: center;
  font-size: 18px;
  margin-left: 50px;
  background: #df3033;
  color: #fff;
}

.shoppingCart .cart-bar .cart-bar-right .btn-primary-disabled {
  float: right;
  width: 200px;
  text-align: center;
  font-size: 18px;
  margin-left: 50px;
  background: #e0e0e0;
  color: #b0b0b0;
}

.shoppingCart .cart-bar .cart-bar-right .btn-primary:hover {
  background-color: #df3033;
}

/* 购物车底部导航条CSS END */
/* 购物车主要内容区CSS END */

/* 购物车为空的时候显示的内容CSS */
.shoppingCart .cart-empty {
  width: 1225px;
  margin: 0 auto;
}

.shoppingCart .cart-empty .empty {
  height: 300px;
  color: #b0b0b0;
  overflow: hidden;
}

.shoppingCart .cart-empty .empty h2 {
  margin: 70px 0 15px;
  font-size: 36px;
}

.shoppingCart .cart-empty .empty p {
  margin: 0 0 20px;
  font-size: 20px;
}

/* 购物车为空的时候显示的内容CSS END */
</style>
